﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SisCRA.Business;
using SisCRA.Classes;

namespace SisCRA.Forms
{
    public partial class FormActivity : Form
    {
        #region -Constructor-

        /// <summary>
        /// Construtor padrão.
        /// </summary>
        public FormActivity()
        {
            try
            {
                InitializeComponent();
                LoadComboBoxSearchDiscipline();
                LoadDataGridViewActivity();
            }
            catch (Exception ex)
            {
                Log.Instance.Error(ex);
                MessageBox.Show(this, "Falha ao buscar dados.\r\nPor favor, verifique suas configurações de conexão com o banco de dados.", "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #endregion

        #region -Events-

        private void comboBoxSearchDiscipline_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                LoadDataGridViewActivity();
            }
            catch
            {

            }
        }

        private void dataGridViewActivity_SelectionChanged(object sender, EventArgs e)
        {
            try
            {
                this.textBoxName.Text = string.Empty;
                this.textBoxDescription.Text = string.Empty;
                this.textBoxPercentage.Text = string.Empty;

                if (this.dataGridViewActivity.SelectedRows.Count == 1)
                {
                    this.textBoxName.Text = this.dataGridViewActivity.SelectedRows[0].Cells[1].Value.ToString();
                    this.textBoxDescription.Text = this.dataGridViewActivity.SelectedRows[0].Cells[2].Value.ToString();
                    this.textBoxPercentage.Text = this.dataGridViewActivity.SelectedRows[0].Cells[3].Value.ToString();
                }
            }
            catch
            {

            }
        }

        private void buttonSearch_Click(object sender, EventArgs e)
        {
            try
            {
                Activity activity = new Activity();
                activity.Name = this.textBoxSearchName.Text;
                activity.DisciplineId = (int)this.comboBoxSearchDiscipline.SelectedValue;

                ActivityBLL activityBLL = new ActivityBLL();
                this.dataGridViewActivity.DataSource = activityBLL.GetActivityByParam(activity);
            }
            catch (Exception ex)
            {
                Log.Instance.Error(ex);
                MessageBox.Show(this, "Falha ao buscar a(s) atividade(s).", "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void buttonNewActivity_Click(object sender, EventArgs e)
        {
            CleanFields();
        }

        private void textBoxPercentage_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(char.IsNumber(e.KeyChar) || char.IsControl(e.KeyChar) || (e.KeyChar.Equals(',') && !textBoxPercentage.Text.Contains(','))))
            {
                e.Handled = true;
            }
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(textBoxName.Text) || string.IsNullOrWhiteSpace(textBoxDescription.Text) || string.IsNullOrWhiteSpace(textBoxPercentage.Text))
                {
                    MessageBox.Show(this, "Existem campos inválidos!", "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    Activity activity = new Activity();
                    if (this.dataGridViewActivity.SelectedRows.Count == 1)
                    {
                        activity.Id = int.Parse(this.dataGridViewActivity.SelectedRows[0].Cells[0].Value.ToString());
                    }
                    activity.Name = this.textBoxName.Text;
                    activity.Description = this.textBoxDescription.Text;
                    activity.Percentage = double.Parse(this.textBoxPercentage.Text);
                    activity.DisciplineId = (int)this.comboBoxSearchDiscipline.SelectedValue;

                    ActivityBLL activityBLL = new ActivityBLL();
                    if (activityBLL.IsPercentageOk(activity))
                    {
                        activityBLL.SaveActivity(activity);

                        CleanFields();
                        LoadDataGridViewActivity();
                    }
                    else
                    {
                        MessageBox.Show(this, "O peso de todas as atividades está ultrapassando 100%!", "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        textBoxPercentage.Text = string.Empty;
                        textBoxPercentage.Focus();
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Instance.Error(ex);
                MessageBox.Show(this, "Falha ao salvar a atividade.", "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        #endregion

        #region -Private Methods-

        /// <summary>
        /// Método que carrega o comboBoxSearchDiscipline.
        /// </summary>
        private void LoadComboBoxSearchDiscipline()
        {
            try
            {
                DisciplineBLL disciplineBLL = new DisciplineBLL();

                this.comboBoxSearchDiscipline.DataSource = disciplineBLL.GetAllDiscipline();
                this.comboBoxSearchDiscipline.ValueMember = "ID";
                this.comboBoxSearchDiscipline.DisplayMember = "CODIGO";
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Método que carrega o DataGridViewActivity.
        /// </summary>
        private void LoadDataGridViewActivity()
        {
            try
            {
                Activity activity = new Activity();
                activity.DisciplineId = (int)this.comboBoxSearchDiscipline.SelectedValue;

                ActivityBLL activityBLL = new ActivityBLL();
                this.dataGridViewActivity.DataSource = activityBLL.GetAllActivityByDiscipline(activity);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Método que limpa os campos do formulário.
        /// </summary>
        private void CleanFields()
        {
            this.textBoxSearchName.Text = string.Empty;
            this.dataGridViewActivity.ClearSelection();
            this.textBoxName.Text = string.Empty;
            this.textBoxDescription.Text = string.Empty;
            this.textBoxPercentage.Text = string.Empty;
            this.textBoxName.Focus();
        }

        #endregion
    }
}
